package com.better.common.jwtauth.secruity;


import com.better.backend.mqReceiverService.UserInfo.model.UserModel;
import com.better.backend.mqReceiverService.UserInfo.repository.UserInfoRepsitory;
import com.better.common.exception.DescribeException;
import com.better.common.exception.ExceptionEnum;
import com.better.common.jwtauth.user.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class JwtUserDetailsServiceImpl implements JwtUserDetailsService {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserInfoRepsitory userInfoRepsitory;

    @Override
    public JWTUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //User user = userRepository.findByUsername(username);
        UserModel userModel= userInfoRepsitory.findByLoginName(username);
        if (userModel == null) {
            throw new DescribeException(ExceptionEnum.USER_NOT_FIND);
        } else {
            return JwtUserFactory.create(userModel);
        }
    }
}
